package com.translate.web.service.impl;

import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.FlowCommentMapper;
import com.translate.entity.FlowComment;
import com.translate.entity.NumberOfAssociatedIdClassificationObtained;
import com.param.contract.IIdParam;
import com.param.contract.IPage;
import com.translate.web.service.IFlowCommentService;
import com.basic.service.impl.BaseServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
public class FlowCommentServiceImpl extends BaseServiceImpl<FlowComment> implements IFlowCommentService {
	
	private RedisTemplate<String, List<?>> redisTemplate;

	
	@Autowired
	public void setRedisTemplate(RedisTemplate<String, List<?>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	public FlowCommentServiceImpl(FlowCommentMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(FlowComment data) {
		long time = System.currentTimeMillis() / 1000;
		data.setCreateTime(time);
		data.setUpdateTime(time);
	}

	protected void updatePropteris(FlowComment data) {
		data.setUpdateTime(System.currentTimeMillis() / 1000);
	}

	protected Class<FlowComment> getEntityClass() {
		return FlowComment.class;
	}
	
	
	protected <S extends IPage> String sqlByGetCounttAfterProcesser(S param) {
		
		IIdParam idParam = (IIdParam) param; 
		
		return this.sqlByGetCount("user_id = " + idParam.getId());
	}
	
	
	protected <S extends IPage> String sqlByGetPageListAfterProcesser(S param) {
		IIdParam idParam = (IIdParam) param; 
		return this.sqlByGetPageList("user_id = " + idParam.getId());
	}
	

	@Override
	public <VO> Map<Integer, Integer> getNumberOfCommentsObtained(List<VO> vos, Function<VO, Integer> function) {
	
		List<NumberOfAssociatedIdClassificationObtained> associatedIdClassificationObtaineds = this.getAssociatedIdClassificationObtaineds(vos, function);
		
		Map<Integer, Integer> map = new HashMap<>(40);
		
		for (NumberOfAssociatedIdClassificationObtained numberOfAssociatedIdClassificationObtained : associatedIdClassificationObtaineds) {
			
			map.put(numberOfAssociatedIdClassificationObtained.getId(), numberOfAssociatedIdClassificationObtained.getNum());
		}
		
		associatedIdClassificationObtaineds.clear();
		
		return map;
	}
	
	private <VO> List<NumberOfAssociatedIdClassificationObtained> getAssociatedIdClassificationObtaineds(List<VO> vos, Function<VO, Integer> function) {
		
		Integer[] ids = vos.stream().map(e->function.apply(e)).toArray(Integer[]::new);
		
		String key = RedisConstant.getAssociatedIdClassificationObtainedsByKey(ids);
		
		
		ValueOperations<String, List<?>> valueOperations = this.redisTemplate.opsForValue();
		
		List<NumberOfAssociatedIdClassificationObtained> order = (List<NumberOfAssociatedIdClassificationObtained>) valueOperations.get(key);
		
		if (null != order) {
			return order;
		}
		
		FlowCommentMapper approvalMapper = this.getBaseMapper();
		
		order = approvalMapper.getNumberOfAssociatedIdClassificationObtained(ids);
		
		if (null == order) {
			order = new ArrayList<NumberOfAssociatedIdClassificationObtained>();
			
			valueOperations.set(key, order, 3, TimeUnit.SECONDS);
			
			return order;
		}
		valueOperations.set(key, order, 6, TimeUnit.SECONDS);

		return order;
	}
}